package com.cyl.manager.yee.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.yee.domain.MerchantStatistics;
import com.cyl.manager.yee.mapper.MerchantStatisticsMapper;
import com.cyl.manager.yee.pojo.query.MerchantStatisticsQuery;
import com.cyl.manager.yee.pojo.vo.YeeMerchantSttatisticsSqlVO;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.date.DateUtil;

/**
 * 费用统计Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class MerchantStatisticsService {
    @Autowired
    private MerchantStatisticsMapper merchantStatisticsMapper;

    /**
     * 查询费用统计
     *
     * @param id 费用统计主键
     * @return 费用统计
     */
    public MerchantStatistics selectById(Long id) {
        return merchantStatisticsMapper.selectById(id);
    }

    public List<MerchantStatistics> selectByEntityToDay(MerchantStatisticsQuery query) {
    	MerchantStatistics merchantStatistics = new MerchantStatistics();
    	merchantStatistics.setMerchantNo(query.getMerchantNo());
    	merchantStatistics.setDeptZbjId(query.getDeptZbjId());
    	merchantStatistics.setTjdata(query.getTjdata());
        return merchantStatisticsMapper.selectByEntityToDay(merchantStatistics);
    }
    public MerchantStatistics getMerchantStatisticsToDay(MerchantStatisticsQuery query) {
        return merchantStatisticsMapper.getMerchantStatisticsToDay(query);

	}
    
    
    public MerchantStatistics getMerchantStatistics(MerchantStatisticsQuery query) {
         QueryWrapper<MerchantStatistics> qw = new QueryWrapper<>();
         
         qw.select(
        		"sum(pay_amount) AS pay_amount,\r\n" + 
         		"sum(pay_fee_amount) AS pay_fee_amount,\r\n" + 
         		"sum(ret_amount) AS ret_amount,\r\n" + 
         		"sum(ret_fee_amount) AS ret_fee_amount,\r\n" + 
         		"sum(kajs) AS kajs,\r\n" + 
         		"sum(kysjs) AS kysjs,\r\n" + 
         		"sum(txjs) AS txjs,\r\n" + 
         		"sum(kajssxf) AS kajssxf,\r\n" + 
         		"sum(kysjssxf) AS kysjssxf,\r\n" + 
         		"sum(txjssxf) AS txjssxf,\r\n" + 
         		"sum(zblljs) AS zblljs,\r\n" + 
         		"sum(yfwqjs) AS yfwqjs ");
    	 Integer[] tjdataRange = query.getTjdataRange();
         if (tjdataRange != null && tjdataRange.length == 2) {
             Integer start = tjdataRange[0];
             if (start != null) {
                 qw.ge("tjdata", start);
             }
             Integer end = tjdataRange[1];
             if (end != null) {
                 qw.le("tjdata", end);
             }
         } 
         Long deptZbjId = query.getDeptZbjId();
         if (deptZbjId != null) {
             qw.eq("dept_zbj_id", deptZbjId);
         }
         return merchantStatisticsMapper.selectOne(qw);

	}
    
    
    
    /**
     * 查询费用统计列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 费用统计
     */
    public List<MerchantStatistics> selectList(MerchantStatisticsQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize(),"tjdata desc");
        }
        QueryWrapper<MerchantStatistics> qw = new QueryWrapper<>();
        
        Integer[] tjdataRange = query.getTjdataRange();
        if (tjdataRange != null && tjdataRange.length == 2) {
            Integer start = tjdataRange[0];
            if (start != null) {
                qw.ge("tjdata", start);
            }
            Integer end = tjdataRange[1];
            if (end != null) {
                qw.le("tjdata", end);
            }
        }
        
        Integer tjdata = query.getTjdata();
        if (tjdata != null) {
            qw.eq("tjdata", tjdata);
        }
        Integer tjyear = query.getTjyear();
        if (tjyear != null) {
            qw.eq("tjyear", tjyear);
        }
        Integer tjmonth = query.getTjmonth();
        if (tjmonth != null) {
            qw.eq("tjmonth", tjmonth);
        }
        Integer tjweek = query.getTjweek();
        if (tjweek != null) {
            qw.eq("tjweek", tjweek);
        }
        Integer tjday = query.getTjday();
        if (tjday != null) {
            qw.eq("tjday", tjday);
        }
        Long deptZbjId = query.getDeptZbjId();
        if (deptZbjId != null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        String merchantNo = query.getMerchantNo();
        if (!StringUtils.isEmpty(merchantNo)) {
            qw.eq("merchant_no", merchantNo);
        }
        BigDecimal payAmount = query.getPayAmount();
        if (payAmount != null) {
            qw.eq("pay_amount", payAmount);
        }
        BigDecimal payFeeAmount = query.getPayFeeAmount();
        if (payFeeAmount != null) {
            qw.eq("pay_fee_amount", payFeeAmount);
        }
        BigDecimal retAmount = query.getRetAmount();
        if (retAmount != null) {
            qw.eq("ret_amount", retAmount);
        }
        BigDecimal retFeeAmount = query.getRetFeeAmount();
        if (retFeeAmount != null) {
            qw.eq("ret_fee_amount", retFeeAmount);
        }
        BigDecimal kajs = query.getKajs();
        if (kajs != null) {
            qw.eq("kajs", kajs);
        }
        BigDecimal kysjs = query.getKysjs();
        if (kysjs != null) {
            qw.eq("kysjs", kysjs);
        }
        BigDecimal txjs = query.getTxjs();
        if (txjs != null) {
            qw.eq("txjs", txjs);
        }
        BigDecimal kajssxf = query.getKajssxf();
        if (kajssxf != null) {
            qw.eq("kajssxf", kajssxf);
        }
        BigDecimal kysjssxf = query.getKysjssxf();
        if (kysjssxf != null) {
            qw.eq("kysjssxf", kysjssxf);
        }
        BigDecimal txjssxf = query.getTxjssxf();
        if (txjssxf != null) {
            qw.eq("txjssxf", txjssxf);
        }
        BigDecimal zblljs = query.getZblljs();
        if (zblljs != null) {
            qw.eq("zblljs", zblljs);
        }
        BigDecimal yfwqjs = query.getYfwqjs();
        if (yfwqjs != null) {
            qw.eq("yfwqjs", yfwqjs);
        }
        return merchantStatisticsMapper.selectList(qw);
    }
   
    
    @Transactional
    public  int insertMerchantStatisticsDay(LocalDateTime strTime,LocalDateTime endTime) {
       	int tjdata = Integer.parseInt(DateUtil.format(strTime, "yyyyMMdd"));
 		int	tjyear = Integer.parseInt(DateUtil.format(strTime, "yyyy"));
 		int	tjmonth = Integer.parseInt(DateUtil.format(strTime, "MM"));
 		int	tjweek = Integer.parseInt(DateUtil.format(strTime, "w"));
 		int	tjday = Integer.parseInt(DateUtil.format(strTime, "dd"));
 		YeeMerchantSttatisticsSqlVO yeeMerchantSifSqlVO = new YeeMerchantSttatisticsSqlVO();
 		yeeMerchantSifSqlVO.setCreateTime(LocalDateTime.now());
    	yeeMerchantSifSqlVO.setStrTime(strTime);
    	yeeMerchantSifSqlVO.setEndTime(endTime);
    	yeeMerchantSifSqlVO.setTjdata(tjdata);
    	yeeMerchantSifSqlVO.setTjyear(tjyear);
    	yeeMerchantSifSqlVO.setTjmonth(tjmonth);
    	yeeMerchantSifSqlVO.setTjweek(tjweek);
    	yeeMerchantSifSqlVO.setTjday(tjday);
    	QueryWrapper<MerchantStatistics> wrapper = new QueryWrapper<MerchantStatistics>();
    	wrapper.eq("tjdata", yeeMerchantSifSqlVO.getTjdata());
    	merchantStatisticsMapper.delete(wrapper);
        return merchantStatisticsMapper.insertMerchantStatisticsDay(yeeMerchantSifSqlVO);
    }

	

	
    
    
}
